package com.example.calender.ui;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.PopupMenu;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.example.calender.BaseActivity;
import com.example.calender.R;
import com.example.calender.adapter.CalenderAdapter;
import com.example.calender.utils.CalendarUtils;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends BaseActivity {

    private ImageView imageCalender;
    private ImageView imageMore;
    private TextView tvYear;
    private TextView tvMoth;
    private TextView tvDay;
    private CalendarView calendarView;
    private int year;
    private int month;
    private CalenderAdapter calenderAdapter;
    private final List<Object> listDta = new ArrayList<>();
    private String lunar = "";
    private List<Calendar.Scheme> schemes = new ArrayList<>();
    private RecyclerView recyclerView;
    private int day;
    private String strEvent;

    @Override
    protected int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void findViews() {
        imageCalender = findViewById(R.id.image_calender);
        imageMore = findViewById(R.id.image_more);
        tvYear = findViewById(R.id.tv_year);
        tvMoth = findViewById(R.id.tv_moth);
        tvDay = findViewById(R.id.tv_day);
        calendarView = findViewById(R.id.calendarView);
        recyclerView = findViewById(R.id.recyclerView);
    }

    @Override
    protected void initView() {
        calenderAdapter = new CalenderAdapter(listDta,this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(calenderAdapter);
    }

    @Override
    protected void initListener() {
        imageCalender.setOnClickListener(v -> {
            if(calendarView.isYearSelectLayoutVisible()){
                calendarView.closeYearSelectLayout();
            }else {
                calendarView.showYearSelectLayout(calendarView.getCurYear());
            }
        });
        calendarView.setOnMonthChangeListener((year, month) -> {
            this.year = year;
            this.month = month;
        });
        calendarView.setOnCalendarSelectListener(new CalendarView.OnCalendarSelectListener() {

            @Override
            public void onCalendarOutOfRange(Calendar calendar) {
                notifyTitle(calendar);
            }

            @Override
            public void onCalendarSelect(Calendar calendar, boolean isClick) {
                notifyTitle(calendar);
            }
        });
        imageMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopup();
            }
        });
        calenderAdapter.setEventListener(new CalenderAdapter.EventListener() {
            @Override
            public void click(String title) {
                EventActivity.start(MainActivity.this,title);
            }
        });
    }

    @Override
    protected void initData() {
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        year=calendar.get(java.util.Calendar.YEAR);
        month=calendar.get(java.util.Calendar.MONTH)+1;
        day =calendar.get(java.util.Calendar.DAY_OF_MONTH);
        calendarView.scrollToCalendar(2021,5,19);
        calendarView.scrollToCalendar(year,month, day);

    }

    @Override
    protected void onStart() {
        super.onStart();
        calendarView.clearSchemeDate();
        calendarView.addSchemeDate(CalendarUtils.findAllEvent());
    }

    private void notifyTitle(Calendar calendar){
        schemes.clear();
        strEvent = calendar.getScheme();
        if(!TextUtils.isEmpty(strEvent)){
            Calendar.Scheme scheme = new Calendar.Scheme();
            scheme.setScheme(strEvent.substring(5));
            scheme.setOther(strEvent.substring(0,5));
            schemes.add(scheme);
        }
        String str = "";
        long millis= System.currentTimeMillis()-calendar.getTimeInMillis();
        long day = millis/(24*60*60*1000);
        if(Math.abs(millis)<12*60*60*1000){
            str="今天";
        }else if(millis<0){
            str=Math.abs(day)+"天后";
        }else {
            str=Math.abs(day)+"天前";
        }
        tvYear.setText(year+"年");
        tvMoth.setText(String.valueOf(month));
        tvDay.setText(str);
        listDta.clear();
        listDta.add(calendar.getLunar()+"  |  "+year+"年"+month+"月"+calendar.getDay()+"日");
        if(schemes!=null){
            for (Calendar.Scheme get :schemes){
                listDta.add(get);
            }
        }
        calenderAdapter.notifyDataSetChanged();
    }

    //菜单栏
    private void showPopup(){
        PopupMenu popupMenu = new PopupMenu(this, imageMore);
        popupMenu.getMenuInflater().inflate(R.menu.meun_arlt, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.time :
                        calendarView();
                        break;
                    case R.id.event :
                        EventActivity.start(MainActivity.this,null);
                        break;
                    default:
                        throw new IllegalStateException("Unexpected value: " + item.getItemId());
                }
                return true;
            }
        });
        popupMenu.show();
    }

    /**
     * 时间选择
     */
    private void calendarView() {
        //时间选择器
        TimePickerView pvTime = new TimePickerBuilder(MainActivity.this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                java.util.Calendar calendar = java.util.Calendar.getInstance();
                calendar.setTime(date);
                year=calendar.get(java.util.Calendar.YEAR);
                month=calendar.get(java.util.Calendar.MONTH)+1;
                day =calendar.get(java.util.Calendar.DAY_OF_MONTH);
                calendarView.scrollToCalendar(year,month, day);
            }
        }).setDividerColor(Color.argb(255,3,169,244))
                .setTitleText("选择跳转时间")
                .setLineSpacingMultiplier(3f)
                .build();
        pvTime.show();
    }

}